package com.dmyang.p2pchat.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dmyang.p2pchat.bean.User;

import java.util.ArrayList;
import java.util.List;

/**
 * 对数据库p2pchat中的userlist表进行crud操作
 * Created by brss on 2017/11/3.
 */

public class UserListDao {
    private MySqlliteOpenHelper mySqlliteOpenHelper;
    private User user;

    public UserListDao(Context context) {
        mySqlliteOpenHelper = new MySqlliteOpenHelper(context, "p2pchat", null, 1);
        this.user = user;
    }

    public void add(User user) {
        SQLiteDatabase db = mySqlliteOpenHelper.getReadableDatabase();
        db.execSQL("insert into userlist(username,ip,port) values(?,?,?);", new Object[]{user.getUsername(), user.getIp(), user.getPort()});
        db.close();

    }

    public void delete(String ip) {
        SQLiteDatabase db = mySqlliteOpenHelper.getReadableDatabase();
        db.execSQL("delete from userlist where ip='?'", new Object[]{ip});
        db.close();
    }

    public List query() {
        SQLiteDatabase db = mySqlliteOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from userlist", null);//无条件查询
        //db.rawQuery("select * from userlist where ip = '?'",new String[]{"192.168.1.102"});//有条件查询的方法
        //解析cursor的数据
        List userList = new ArrayList();
        if (cursor!=null && cursor.getCount()>0){//判断cursor中是否有数据
            while (cursor.moveToNext()){
                int id = cursor.getInt(0);
                String username = cursor.getString(1);
                String ip = cursor.getString(2);
                int port = cursor.getInt(3);
                User user = new User(username,ip,port);
                userList.add(user);
                //System.out.println("id:"+id+" username:"+username+" ip"+ip+" port:"+port);
            }
            cursor.close();
        }
        db.close();
        return userList;
    }

    public List queryByIp(String theIp) {
        SQLiteDatabase db = mySqlliteOpenHelper.getReadableDatabase();
        String[] strings = new String[1];
        strings[0] = theIp;
        Cursor cursor = db.rawQuery("select * from userlist where ip=?;",strings);//无条件查询
        //db.rawQuery("select * from userlist where ip = '?'",new String[]{"192.168.1.102"});//有条件查询的方法
        //解析cursor的数据
        List userList = new ArrayList();
        if (cursor!=null && cursor.getCount()>0){//判断cursor中是否有数据
            while (cursor.moveToNext()){
                int id = cursor.getInt(0);
                String username = cursor.getString(1);
                String ip = cursor.getString(2);
                int port = cursor.getInt(3);
                User user = new User(username,ip,port);
                userList.add(user);
                //System.out.println("id:"+id+" username:"+username+" ip"+ip+" port:"+port);
            }
            cursor.close();
        }
        db.close();
        return userList;
    }

}
